// console.log(process.env.NODE_ENV)
let current = process.env.NODE_ENV;
console.log("当前环境:" + current)
const PRODUCT = "production"
const config = {
  // baseUrl: "http://question.clovsoft.com:10028"
  baseUrl: current === PRODUCT ? window.origin : "http://127.0.0.1:10010"
  // baseUrl: window.origin
}
const instance = axios.create({
  baseURL: config.baseUrl,
  headers: {'Content-Type': 'application/json'}
});
const createRequest = (request) => {
  return new Promise(resolve => {
    request
      .then(e => {
        if (e.status !== 200) {

          return resolve({
            code: e.code,
            message: "请求失败"
          })
        }
        resolve(e.data);
      })
      .catch(e => {
        console.error(e)
        resolve({
          code: 400,
          message: "请求失败"
        })
      })
  })
}
export default {
  /**
   * 获取答题卡
   * @param data
   * @returns {Promise<unknown>}
   */
  getSheet: (data) => {
    return createRequest(instance.post("/smart-question-api/bs-paper/v1/get_sheet", data))
  },
  /**
   * 保存或更新答题卡
   * @param data
   * @returns {Promise<unknown>}
   */
  saveOrUpdateSheet: (data) => {
    return createRequest(instance.post("/smart-question-api/bs-paper/v1/save_or_update_sheet", data))
  },
  /**
   * 上传文件
   * @param data
   * @returns {Promise<unknown>}
   */
  saveFile: (data) => {
    return createRequest(instance.post("/file/v1/upload", data, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }))
  },
  uploadZip: (file, params, callback) => {
    let formData = new FormData();
    formData.append("file", file);
    formData.append("params", JSON.stringify(params))
    formData.append("callback", callback)
    formData.append("successCode", "200")
    formData.append("statusParamsName", "code")

    return createRequest(instance.post('/file/v2/upload_zip', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }))
  },
  /**
   * 获取试卷
   * @param data
   * @returns {Promise<unknown>}
   */
  requestQuestions: (data) => {
    return createRequest(instance.post("/smart-question-api/bs-paper-question/v2/get_basket", data))
  },
  /**
   * 查看配置和题目列表
   * @param data
   * @returns {Promise<unknown>}
   */
  parseConfigAndQuestions: (data) => {
    return createRequest(instance.post('/smart-question-api/parse-paper/v1/all_question', data))
  },
  /**
   * 保存解析配置
   * @param data
   * @returns {Promise<unknown>}
   */
  saveParseConfig: (data) => {
    return createRequest(instance.post('/smart-question-api/parse-paper/v1/save', data))
  },
  /**
   * 字节转base64
   * @param data
   * @returns {Promise<unknown>}
   */
  getBase64FromUrl: (data) => {
    return createRequest(instance.post("/paper-parse-api/v1/toBase64", data))
  },
  /**
   * 轮廓检测
   * @param data
   * @returns {Promise<unknown>}
   */
  findItemInImgs: (data) => {
    return createRequest(instance.post("/paper-parse-api/v1/find_item_in_template", data, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }))
  },
  /**
   * 根据配置找到选项列表
   * @param file
   * @param config
   * @returns {Promise<unknown>}
   */
  matchTemplate: (file, config) => {
    const data = new FormData();
    data.append("part", file)
    data.append("config", JSON.stringify(config))
    return createRequest(instance.post("/paper-parse-api/v2/matchTemplate", data, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }))
  },
  changeParseEnabled(data) {
    return createRequest(instance.post("/smart-question-api/parse-paper/v1/update_status", data))
  },
  createParse(data) {
    return createRequest(instance.post("/smart-question-api/parse-paper/v1/save", data))
  },
  deleteTemplate(data) {
    return createRequest(instance.post("/smart-question-api/parse-paper/v1/remove_template", data))
  },
  workOne(data) {
    return createRequest(instance.post("/smart-question-api/bs-work/v1/one", data))
  },
  parseList(data) {
    return createRequest(instance.post("/smart-question-api/parse-paper/v1/list", data))
  },
  getTemplateUrl(templateId) {
    return `${instance.defaults.baseURL}/paper-parse-api/img/template?templateId=${templateId}`
  },
  deleteSheet(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/delete_sheet", data))
  },
  examStudentUploadPage(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/list_students", data))
  },
  editUploadTotal(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/error_number", data))
  },
  uploadDetail(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/upload_detail", data))
  },
  errorCodePage(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/list_error_code", data))
  },
  errorQuestionPage(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/list_error_objectives", data))
  },
  errorQuestionHandler(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/update_answer", data))
  },
  errorCodeHandler(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/update_code", data))
  },
  uploadHttpUrls(data) {
    return createRequest(instance.post("/paper-parse-api/v1/upload_httpUrls", data))
  },
  parseOkList(data) {
    return createRequest(instance.post("/smart-question-api/parse-paper/v1/list_ok", data))
  },
  uploadImagePage(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/image_page", data))
  },
  commitUpload(data) {
    return createRequest(instance.post("/smart-question-api/bs-work-editor-upload/v1/commit", data))
  },
}